package com.emm.yixun.mobile.adapter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.model.GetCustomerDetail.FamilyMemberList;
import com.eroad.product.tools.Time_SelectPicPopupWindow;

public class BirtchdayAdapter extends BaseAdapter {

	int positions = 0;

	EditText ed;

	Activity context;

	ArrayList<FamilyMemberList> bl;

	private Integer index2 = -1;

	/** 自定义时间控件 */
	private Time_SelectPicPopupWindow time_SelectPicPopupWindow;

	View view;

	int type = 0;

	public BirtchdayAdapter(Activity context, ArrayList<FamilyMemberList> bl, View view) {
		this.context = context;
		this.bl = bl;
		this.view = view;
	}

	public void notifylist(ArrayList<FamilyMemberList> bl) {
		this.bl = bl;
		notifyDataSetChanged();
	}

	public void SetType(int type) {
		this.type = type;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return bl == null ? 0 : bl.size();
	}

	@Override
	public Object getItem(int position) {
		return bl == null ? null : bl.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.birtchday_item, null);
			v = new ViewHoudler();
			v.title = (TextView) convertView.findViewById(R.id.title);
			v.name_bir_ed = (EditText) convertView.findViewById(R.id.name_bir_ed);
			v.bir_time = (EditText) convertView.findViewById(R.id.bir_time);
			v.delete_btn = (ImageView) convertView.findViewById(R.id.delete_btn);
			v.all_liean = (LinearLayout) convertView.findViewById(R.id.all_liean);
			v.right_btn = (ImageView) convertView.findViewById(R.id.right_btn);
			convertView.setTag(v);
		} else {
			v = (ViewHoudler) convertView.getTag();
		}
		v.title.setText("家庭成员" + bl.get(position).getCount());
		if (type == 0) {
			v.name_bir_ed.setEnabled(true);
			v.bir_time.setEnabled(true);
			v.delete_btn.setVisibility(View.VISIBLE);
			v.right_btn.setVisibility(View.VISIBLE);
		} else {
			v.name_bir_ed.setEnabled(false);
			v.bir_time.setEnabled(false);
			v.delete_btn.setVisibility(View.INVISIBLE);
			v.right_btn.setVisibility(View.GONE);
		}
		v.delete_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 删除成员
				bl.remove(position);
				notifyDataSetChanged();
			}
		});
		v.name_bir_ed.setTag(position);
		v.name_bir_ed.setText(bl.get(position).getMemberName());
		v.name_bir_ed.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					index2 = (Integer) v.getTag();
				}
				return false;
			}
		});
		class MyTextWatcher2 implements TextWatcher {
			public MyTextWatcher2(ViewHoudler holder) {
				mHolder = holder;
			}

			private ViewHoudler mHolder;

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				if (s != null && !"".equals(s.toString())) {
					int position = (Integer) mHolder.name_bir_ed.getTag();

					bl.get(position).setMemberName(s.toString());
				}
			}
		}
		v.name_bir_ed.addTextChangedListener(new MyTextWatcher2(v));

		v.all_liean.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View g) {
				// 调用时间选择器
				EmmApplication.disKey(context);
				time_SelectPicPopupWindow = new Time_SelectPicPopupWindow(context, keyListener, 0, GetDate(v.bir_time));
				time_SelectPicPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
				positions = position;
			}
		});
		ed = v.bir_time;
		v.bir_time.setText(bl.get(position).getBirthday() == null ? "" : bl.get(position).getBirthday()+" ");
		v.bir_time.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View g) {
				// 调用时间选择器
				EmmApplication.disKey(context);
				time_SelectPicPopupWindow = new Time_SelectPicPopupWindow(context, keyListener, 0, GetDate(v.bir_time));
				time_SelectPicPopupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
				positions = position;
			}
		});

		return convertView;
	}

	ViewHoudler v = null;

	public static class ViewHoudler {

		/** title */
		TextView title;

		/** 名称 */
		EditText name_bir_ed;

		/** 时间 */
		EditText bir_time;

		/** 删除按钮 */
		ImageView delete_btn;

		LinearLayout all_liean;

		ImageView right_btn;

	}

	OnClickListener keyListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.imageView1:
				time_SelectPicPopupWindow.dismiss();
				time_SelectPicPopupWindow.Aprils.clear();
				time_SelectPicPopupWindow.Years.clear();
				time_SelectPicPopupWindow.Days.clear();

				SimpleDateFormat formats = new SimpleDateFormat("yyyy年MM月dd日");
				Calendar c = Calendar.getInstance();
				try {
					c.setTime(formats.parse(time_SelectPicPopupWindow.getLanguage() + time_SelectPicPopupWindow.getType_One()
							+ time_SelectPicPopupWindow.getType()));
				} catch (ParseException e) {
					e.printStackTrace();
				}

				Date endDate = c.getTime();
				String str = formats.format(endDate);
				Log.w("家庭成员时间选择器：", "str:" + str);
				bl.get(positions).setBirthday(str);
				notifyDataSetChanged();
				break;
			default:
				break;
			}
		}
	};

	private Date GetDate(EditText view) {
		if (null != view.getText().toString().trim() && !"".equals(view.getText().toString().trim())
				&& !"null".equals(view.getText().toString().trim())) {
			DateFormat fmt = new SimpleDateFormat("yyyy年MM月dd日");
			Date date = null;
			try {
				date = fmt.parse(view.getText().toString().trim());
			} catch (ParseException e) {
				date = new Date(System.currentTimeMillis());
				e.printStackTrace();
			}
			return date;
		} else {
			return new Date(System.currentTimeMillis());
		}
	}
}
